package com.candoit.webapps.pricing.controllers;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

import com.candoit.webapps.pricing.bean.PricingContext;
import com.candoit.webapps.pricing.model.User;
import com.candoit.webapps.pricing.util.QueryUtil;

@ManagedBean(name = "appUser")
@SessionScoped
public class AplicationUser implements Serializable {

	private static final long serialVersionUID = -6813537138855230965L;
	
	private User userLogin;
	
	private String userName;
	
	private String password;
	
	private boolean userDontFound;
	
	private boolean actualizePassword = false;
	
	private String newPassword;
	
	private String confirmNewPassword;
	
	private boolean passwordIncorrect = false;
	
	private boolean userDontFoundActualize = false;
	
	public User getUserLogin() {
		return userLogin;
	}

	public void setUserLogin(User userLogin) {
		this.userLogin = userLogin;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public boolean isUserDontFound() {
		return userDontFound;
	}

	public void setUserDontFound(boolean userDontFound) {
		this.userDontFound = userDontFound;
	}
	
	public boolean isActualizePassword() {
		return actualizePassword;
	}

	public void setActualizePassword(boolean actualizePassword) {
		this.actualizePassword = actualizePassword;
	}
	
	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmNewPassword() {
		return confirmNewPassword;
	}

	public void setConfirmNewPassword(String confirmNewPassword) {
		this.confirmNewPassword = confirmNewPassword;
	}
	
	public boolean isPasswordIncorrect() {
		return passwordIncorrect;
	}

	public void setPasswordIncorrect(boolean passwordIncorrect) {
		this.passwordIncorrect = passwordIncorrect;
	}
	
	public List<User> getUsers() {
		List<User> users = QueryUtil.getInstance().getUsers();
		return users;
	}
	
	public boolean isUserDontFoundActualize() {
		return userDontFoundActualize;
	}

	public void setUserDontFoundActualize(boolean userDontFoundActualize) {
		this.userDontFoundActualize = userDontFoundActualize;
	}

	public void loginUser(){
		EntityManager em = PricingContext.getInstance().getEntityManager();
		try{
			User user = (User) em.createQuery("select u from User u where u.userName = :userName and u.keyWord = :password ")
							   .setParameter("userName", userName).setParameter("password", password).getSingleResult();		
			userLogin = user;
		}catch(NoResultException e){
			userDontFound = true;
		}catch(Exception e){
			FacesContext.getCurrentInstance().addMessage("loginform:button", new FacesMessage("Error de comunicación con la Base de datos. Contacte al responsable de la aplicación o intente nuevamente"));
		}
	}
	
	public void logoutUser(){
		clearData();
	}
	
	public void changePassword(){
		actualizePassword = true;
		userLogin = null; 
		userName = null;
		password = null;
		newPassword = null;
		confirmNewPassword = null;
		userDontFound = false;
		userDontFoundActualize = false;
	}
	
	public void cancelChangePassword(){
		actualizePassword = false;
		clearData();
	}
	
	public void changePasswordConfirm(){
		EntityManager em = PricingContext.getInstance().getEntityManager();
		try{
			User user = (User) em.createQuery("select u from User u where u.userName = :userName and u.keyWord = :password ")
							   .setParameter("userName", userName).setParameter("password", password).getSingleResult();		
			if (newPassword.equals(confirmNewPassword)){
				user.setKeyWord(newPassword);
				em.merge(user);
				em.getTransaction().begin();
				em.getTransaction().commit();
			}else{
				passwordIncorrect = true;
				userDontFound = false;
			}
		}catch(NoResultException e){
			userDontFoundActualize = true;
			passwordIncorrect = false;
		}
	}
	
	public void clearData(){
		userLogin = null; 
		userName = null;
		password = null;
		newPassword = null;
		confirmNewPassword = null;
		actualizePassword = false;
		userDontFound = false;
	}
	

}
